//
//  ProvinceAndCItyViewController.m
//  09-HomeWork
//
//  Created by dllo on 16/7/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ProvinceAndCItyViewController.h"
#import "ZoneViewController.h"
#define kTableViewCellReuse @"reuse"
@interface ProvinceAndCItyViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *provinceArray;
@end

@implementation ProvinceAndCItyViewController
- (void)viewDidLoad {
    self.navigationItem.title = @"省份－市区";
    [super viewDidLoad];
    [self loadData];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableViewCellReuse];
}
#pragma mark - 点击推出区页面方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //取出对应得省
    NSMutableDictionary *provinceDic = _provinceArray[indexPath.section];
    //取出provinceDic 的市数组
    NSMutableArray *cityArray = [provinceDic objectForKey:@"cityArray"];
   // 从cityArray 取出是字典
    NSMutableDictionary *cityDic = cityArray[indexPath.row];
  // 从cityDic 取出 区数组
    NSMutableArray *zoneArr = [cityDic objectForKey:@"zoneArray"];
    ZoneViewController *zoneVC = [[ZoneViewController alloc] init];
    zoneVC.cityName = [cityDic objectForKey:@"cityName"];
    zoneVC.zoneArray = zoneArr;
    [self.navigationController pushViewController:zoneVC animated:YES];
}
#pragma mark - dataSource
//有多少个市就有多少个section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _provinceArray.count;
}
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
   //取出section对应得省字典
    NSMutableDictionary *provinceDic = _provinceArray[section];
   //从provinceDic 取出 省名作为section的头标题
    NSString *provinceName = [provinceDic objectForKey:@"provinceName"];
    return provinceName;
}
// 一个省有多少市就显示多好行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     //取出对应得省
    NSMutableDictionary *provinceDic = _provinceArray[section];
    //取出provinceDic 的市数组
    NSMutableArray *cityArray = [provinceDic objectForKey:@"cityArray"];
    return cityArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellReuse];
    //取出对应得省
    NSMutableDictionary *provinceDic = _provinceArray[indexPath.section];
    //取出provinceDic 的市数组
    NSMutableArray *cityArray = [provinceDic objectForKey:@"cityArray"];
    //取出cityArray 的cityDic
    NSMutableDictionary *cityDic = cityArray[indexPath.row];
    cell.textLabel.text =  [cityDic objectForKey:@"cityName"];
    return cell;
}
#pragma mark - loadData
- (void)loadData{
    _provinceArray = [NSMutableArray array];
    NSString *sourceString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"area" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];
    NSArray *sourceArray = [sourceString componentsSeparatedByString:@"\n"];
    for (NSString *string in sourceArray) {
        if (![string hasPrefix:@" "]) {
            //是省名
            NSMutableDictionary *provinceDic = [NSMutableDictionary dictionary];
            //存省名
            [provinceDic setObject:string forKey:@"provinceName"];
            // 创建市数组并存储
            NSMutableArray *cityArray = [NSMutableArray array];
            [provinceDic setObject:cityArray forKey:@"cityArray"];
            [_provinceArray addObject:provinceDic];
        } else if ([string hasPrefix:@"  "] && ![string hasPrefix:@"    "]) {
            //是市名
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
            [cityDic setObject:string forKey:@"cityName"];
            NSMutableArray *zoneArray = [NSMutableArray array];
            [cityDic setObject:zoneArray forKey:@"zoneArray"];
            
            NSMutableArray *cityArray = [[_provinceArray lastObject] objectForKey:@"cityArray"];
            [cityArray addObject:cityDic];
        } else {
            // 是区名
            NSMutableArray *cityArray = [[_provinceArray lastObject] objectForKey:@"cityArray"];
            NSMutableArray *zoneArray = [[cityArray lastObject] objectForKey:@"zoneArray"];
            [zoneArray addObject:string];
        }
    }
  
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//gyjklfkiylifyledlo
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
